<?php 
session_start();
require_once('../include/connect.inc.php');
$r_teacher_id=$_SESSION['id'];
$type=$_POST[type];
if($type==5){
	$max = mysql_query("select max(match_set) from tb_question");
	$total_max = mysql_result($max,0);
	$now_match_set = $total_max+1;
	//ส่วนของการเพิ่มคำถามจับคู่แบบชุด
	$topic=$_POST[topic];
	$level_q_id=$_POST[level_q_id];
	$question=$_POST[num_ques];
	$choice=$_POST[num_cho];		
	$ques=$_POST[question];
	$cho=$_POST[choice];
		for($i=0;$i<=$num_cho-1;$i++){
			if($i<=$question-1){
				$strSql="INSERT INTO tb_question (topic_id,type_id,question_detail,level_q_id,match_set,r_teacher_id) VALUES ('$topic','$type','".$ques[$i]."','$level_q_id','$now_match_set','$r_teacher_id')";
					execute($strSql);
					$query= query("SELECT question_id FROM tb_question order by question_id desc limit 0,1");
						while ($rs = fetch_array($query)){
							$now_q_id=$rs['question_id'];
							}
				$strSql="INSERT INTO tb_choice (choice_detail,question_id,answer,match_set) VALUES ('".$cho[$i]."','$now_q_id','$now_q_id','$now_match_set')";
					execute($strSql);
			}else{
				$strSql="INSERT INTO tb_choice (choice_detail,question_id,match_set) VALUES ('".$cho[$i]."','$now_q_id','$now_match_set')";
					execute($strSql);
		}
	}
}else{
$selec=$_POST[select];
	if ($selec==1){
		$_POST[select1]=1;
	}if ($selec==2){
		$_POST[select2]=1;
	}if ($selec==3){
		$_POST[select3]=1;
	}if ($selec==4){
		$_POST[select4]=1;
	}if ($selec==5){
		$_POST[select5]=1;
	}if ($selec==6){
		$_POST[select6]=1;
	}
//เพิ่มข้อสอบ ทั้งแบบเลือกได้เพียงข้อเดียว และเลือกได้หลายข้อ (เพิ่มเหมือนกัน ต่างกันตอนตรวจข้อสอบ)
		if($_POST[cho]==0){
		$sum=1;
		}else{
		$sum=$_POST[cho];
		}
		for ($number=1;$number<=$sum;$number++){
			if ($number==1){
				$strSql="INSERT INTO tb_choice (question_id,choice_detail,answer) VALUES ('".$_POST[seng]."','".$_POST[choice1]."','".$_POST[select1]."')";
				execute($strSql);
			}
			if ($number==2){
				$strSql="INSERT INTO tb_choice (question_id,choice_detail,answer) VALUES ('".$_POST[seng]."','".$_POST[choice2]."','".$_POST[select2]."')";
				execute($strSql);
			}
			if ($number==3){
				$strSql="INSERT INTO tb_choice (question_id,choice_detail,answer) VALUES ('".$_POST[seng]."','".$_POST[choice3]."','".$_POST[select3]."')";
				execute($strSql);
			}
			if ($number==4){
				$strSql="INSERT INTO tb_choice (question_id,choice_detail,answer) VALUES ('".$_POST[seng]."','".$_POST[choice4]."','".$_POST[select4]."')";
				execute($strSql);
			}
			if ($number==5){
				$strSql="INSERT INTO tb_choice (question_id,choice_detail,answer) VALUES ('".$_POST[seng]."','".$_POST[choice5]."','".$_POST[select5]."')";
				execute($strSql);
			}
			if ($number==6){
				$strSql="INSERT INTO tb_choice (question_id,choice_detail,answer) VALUES ('".$_POST[seng]."','".$_POST[choice6]."','".$_POST[select6]."')";
				execute($strSql);
			}
		}
	}
header("location:frm_cre_question.php?action=addTopic");
?>